﻿using BookFly.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.Input;
using System.Data;

namespace BookFly.ViewModels
{
    internal partial class MainWindowViewModel : ObservableObject
    {

        [ObservableProperty]
        private string _srcCity;

        [ObservableProperty]
        private string _srcCityCode;

        [ObservableProperty]
        private string _dstCityCode = "HRB";

        [ObservableProperty]
        private bool _isForeign = false;

        public ObservableCollection<string> CityAirportCommonCodes { get; set; } = new ObservableCollection<string>();

        FlightInformationService FlightInformationService { get; set; }

        private DataTable _flightPriceTable ;
        public DataTable FlightPriceTable
        {
            get => _flightPriceTable;
            //set
            //{
            //    _flightPriceTable = value;
            //    OnPropertyChanged(nameof(FlightPriceTable));
            //}
            set => SetProperty(ref _flightPriceTable, value);
        }

        [RelayCommand]
        public void AddRow()
        {
            var newRow = FlightPriceTable.NewRow();
            newRow[0] = $"New Data {FlightPriceTable.Rows.Count + 1}";
            newRow[1] = FlightPriceTable.Rows.Count + 3;
            newRow[2] = FlightPriceTable.Rows.Count + 4;
            FlightPriceTable.Rows.Add(newRow);
        }

        [RelayCommand]
        public async Task GetFlightWay()
        {
            FlightInformationService.ClearFlightPrice();
            _srcCityCode = FlightInformationService.CityAirportCommonCodes[_srcCity];
            if (!string.IsNullOrEmpty(_srcCityCode))
            {
                FlightInformationService.datePrices.Clear();
                Dictionary<string, string>.ValueCollection cityAirportCommonCodes;
                if (_isForeign)
                {
                    cityAirportCommonCodes = FlightInformationService.CityAirportForeignCodes.Values;

                    foreach (var code in cityAirportCommonCodes)
                    {
                        if (code != _srcCityCode)
                        {
                            _dstCityCode = code;
                            await FlightInformationService.GetInternationalFlightPrice(_srcCityCode, _dstCityCode);
                        }
                    }
                    
                }
                else
                {
                    cityAirportCommonCodes = FlightInformationService.CityAirportCommonCodes.Values;
                    foreach (var code in cityAirportCommonCodes)
                    {
                        if (code != _srcCityCode)
                        {
                            _dstCityCode = code;
                            await FlightInformationService.GetFlightPrice(_srcCityCode, _dstCityCode);
                        }
                    }

                    
                }

            }

            ShowDataTable();
        }

        private void ShowDataTable()
        {
            //清空表格
            FlightPriceTable.Rows.Clear();
            FlightPriceTable.Clear();             

            //添加数据     
            foreach (var item in FlightInformationService.datePrices) 
            {
                if (item.Value.Prices.Count == 0)
                    continue;

                DataRow row = FlightPriceTable.NewRow();

                row[0] = item.Value.Name; // 目标城市
                int i = 1;
                foreach (var column in FlightPriceTable.Columns)
                {
                    if (column.ToString() == "目标城市")
                        continue;
                    var result = item.Value.Prices.Where(x => x.Key == column.ToString()).FirstOrDefault();

                    row[i] = result.Value;
                    i++;
                }

                FlightPriceTable.Rows.Add(row);
            }
        }

        public MainWindowViewModel() {
            FlightPriceTable = new DataTable();
            FlightInformationService = new FlightInformationService();
            _srcCity = FlightInformationService.CityAirportCommonCodes.Keys.First();
            foreach (var item in FlightInformationService.CityAirportCommonCodes.Keys)
            {
                CityAirportCommonCodes.Add(item);
            };
        }

    }
}
